package son.nt.androideasy;

import java.util.List;

import android.app.Activity;
import android.app.TabActivity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.widget.TabHost;

public class MainActivity extends TabActivity{
	private static final String CATEGORY_ACT1 = MainActivity.class.getPackage().getName() + ".ACT1";
	private static final String TAG = "MainActivity";
	private TabHost tabHost;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		Log.v(TAG, "oncreate");
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		tabHost = getTabHost();
		final PackageManager pm = getPackageManager();
		Intent act1Intent = new Intent(Intent.ACTION_MAIN, null);
		act1Intent.addCategory(CATEGORY_ACT1);
		
		List<ResolveInfo> list = pm.queryIntentActivities(act1Intent, 0);
		for(ResolveInfo info : list){
			TabItem tabItem = new TabItem();
			tabItem.tag = info.activityInfo.name;
			tabItem.label = info.loadLabel(pm);
			tabItem.icon = info.activityInfo.loadIcon(pm);
			tabItem.intent = new Intent()
				.setComponent(new ComponentName(info.activityInfo.applicationInfo.packageName,
						info.activityInfo.name));
			setupTab(tabItem);
		}
		
	}
	
	private void setupTab(TabItem tabItem) {
		TabHost tabHost = getTabHost();
		TabHost.TabSpec spec = tabHost.newTabSpec(tabItem.tag);
		spec.setIndicator(tabItem.label);
		spec.setContent(tabItem.intent);
		tabHost.addTab(spec);
	}

	private static class TabItem{
		private String tag = null;
    	private CharSequence label = null;
    	private Drawable icon = null;
    	private Intent intent = null;
	}
}
